<?

require_once  $_SERVER['DOCUMENT_ROOT']."/../_lib/base/common.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.hotel_instances_you.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.destinations.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.hotels.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.template.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.tree.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.photos.php";   
require_once SITE_ROOT_PATH."/_lib/classes/class.upload.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.suppliers.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.user_auth.php";

	// See if user is logged in 
	$user_auth = new user_auth();
	$user_auth->user_auth_cp();
	
	
	
	$template = new template();





// FUNCTION // Display the suppliers description
function display_description(){

  $suppliers = new suppliers();
  $youtravel = $suppliers->get("you");  
  $params = array("hotel_id"=>intval($_GET['HID']));

  $xml = $youtravel->get_hotel_details($params);

  $data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);

  if($data->Success=="True") {
    $values = array();
    $values["name"] = (string)$data->Hotel["Name"];
    $values["destination"] = (string)$data->Destination;
    $values['hotel_rating'] = (integer)$data->Hotel->Official_Rating;
    $values['hotel_id_youtravel'] = intval($data->HID);
    $values['description'] = (string)$data->Hotel->Hotel_Desc;
    $facilities = "Facilities\n";
    foreach($data->Hotel->Hotel_Facilities->Facility as $facility) {
      $facilities.="* ".(string)$facility."\n";
    }
    $values['facilities'] = $facilities;
    $values['description'].="\n".$facilities;
    $photos = array();
    foreach($data->Hotel->Hotel_Photos->Photo as $photo) {
      $photos[] = (string)$photo;
    }
    $values['photos'] = $photos;
  }
  else {
    // YouTravel error
    $GLOBALS["note"] = array("type"=>"neg","message"=>(string)$data->ErrorMsg);
  }

/*
The url for the hotel description: http://testxml.youtravel.com/webservicestest/get_hoteldetails.asp?LangID=EN&HID=2993&Username=xmltestme&Password=testme

Convert all xml data to a PHP array and call it "$values"


$values['description'] will be the data in the XML node called "Hotel_Desc".

The "Hotel_Facilities" node and data should be parsed into the following format:

Facillities
* Facility 1
* Facility 2

Once it is parsed it can be merged with the data in the $values['description'] variable.

Then add the "$values" array to a new session array so we can use it later.
$_SESSION['hotel_download'] = $values;


*/

// Sample data
/*
$values = array();
$values['name'] = "Marco Polo 2";
$values['destination'] = "Ibiza - San Antonio";
$values['hotel_rating'] = "3";
$values['hotel_id_youtravel'] = "2993";
$values['description'] = "The Marco Polo 2 is situated at 100 m from the main beach of San Antonio and 200 m from the centre, surrounded by restaurants, bars and other attractions. A very short walk away, there is the beach and the famous zone of sunset bars, with its marvellous sea view. All rooms are modernly furnished and equipped with air conditioning, complete bathroom, satellite TV,  mini-fridge, safe, telephone and balcony. The hotel amenities include among others a fresh-water swimming-pool. Also available for guests are Jacuzzi, sauna, restaurant, bar, pool and table tennis in Marco Polo I. The air-conditioned dining-room, in Marco Polo I, offers a rich international buffet, in generous variety. Lobby Bar offering drinks and snacks. The distance from the airport is approximately 16 km.";
$values['facilities'] = "Facilities
* 24hrs Reception
* Bar(s)
* Bicycle Rental
* Buffet Restaurant
* Car Rental
* Currency Exchange Service
* Jacuzzi
* Laundry Service
* Parking
* Ping-Pong
* Restaurant(s)
* Safety Box
* Snack Bar
* Swimming Pool Outdoor - Fresh Water
* Taxi Service";

$values['photos'][] = "http://testxml.youtravel.com/photos/2993/xml.jpg";
$values['photos'][] = "http://testxml.youtravel.com/photos/2993/Main.jpg";
$values['photos'][] = "http://testxml.youtravel.com/photos/2993/Room.jpg";
*/
$_SESSION['hotel_download'] = $values;
		
?>


<h2>YouTravel(YOU) Hotel Description</h2>

<h3>Description details</h3>


<?
if(!empty($GLOBALS['note'])){
	echo display_message($GLOBALS['note']);
}
?>




<div class="border_box">


<fieldset><legend>General</legend></fieldset>


<table class="libra">
<tr>
    <td width="200"><strong>Hotel name</strong></td>
    <td><?= $values['name'] ?></td>
  </tr>
  <tr>
    <td><strong>Youtravel hotel ID</strong></td>
    <td><?= $values['hotel_id_youtravel'] ?></td>
  </tr>
  <tr>
    <td><strong>Official rating</strong></td>
    <td><?= $values['hotel_rating'] ?></td>
  </tr>
  <tr>
    <td><strong>Youtravel description & facilities</strong></td>
    <td><?= nl2br($values['description']) ?></td>
  </tr>
  <tr>
    <td><strong>Destination</strong></td>
    <td><?= $values['destination'] ?></td>
  </tr>
  <tr>
    <td><strong>Hotel photos</strong></td>
    <td><?
	
	

	foreach($values['photos'] as $photo_values){
		echo "<img src='".$photo_values."' />";
	}
	
	
	
	?></td>
  </tr>
</table>

<br />



<fieldset><legend>Append description to resort</legend></fieldset>


<form action="<?= $_SERVER['PHP_SELF'] ?>" method="get" >

<input type="hidden" name="action" value="confirm" />
<input type="hidden" name="instance_id" value="<?= $_GET['instance_id'] ?>" />

<table class="libra">
<tr>
<td width="200"><strong>Resort tree ID</strong></td>
<td><input type="text" name="parent_tree_id" value="" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="submit"/></td>
</tr>
</table>


</form>

</div>



<?



}
// END FUNCTION









// FUNCTION // Display the suppliers description
function display_confirm_page($resort_values){


	$values = $_SESSION['hotel_download'];



		
?>


<h2>YouTravel(YOU) Hotel Description</h2>


<h3>Confirm</h3>




<div class="border_box">


<fieldset><legend>General</legend></fieldset>


<table class="libra">
<tr>
    <td width="200"><strong>Hotel name</strong></td>
    <td><?= $values['name'] ?></td>
  </tr>
  <tr>
    <td><strong>Youtravel hotel ID</strong></td>
    <td><?= $values['hotel_id_youtravel'] ?></td>
  </tr>
  <tr>
    <td><strong>Official rating</strong></td>
    <td><?= $values['hotel_rating'] ?></td>
  </tr>
  <tr>
    <td><strong>Youtravel description & facilities</strong></td>
    <td><?= nl2br($values['description']) ?></td>
  </tr>
  <tr>
    <td><strong>Destination</strong></td>
    <td><?= $values['destination'] ?></td>
  </tr>
  <tr>
    <td><strong>Hotel photos</strong></td>
    <td><?
	
	
	foreach($values['photos'] as $photo_values){
		echo "<img src='".$photo_values."' />";
	}
	
	
	
	?></td>
  </tr>
</table>

<br />


<fieldset><legend>Confirm append description to resort</legend></fieldset>




<table class="libra">
<tr>
<td width="200"><strong>Resort name</strong></td>
<td><?= $resort_values['name'] ?></td>
</tr>
<tr>
<td><strong>Resort tree ID</strong></td>
<td><?= $resort_values['tree_id'] ?></td>
</tr>
<tr>
<td><strong>Parent destination</strong></td>
<td><?

	$destinations = new destinations();
	$params['tree_id'] = $resort_values['parent_id'];
	$parent_values = $destinations->get_destination($params);
	
	echo $parent_values['name']." (".$parent_values['tree_id'].")";
 

?></td>
</tr>
</table>


<form action="<?= $_SERVER['PHP_SELF'] ?>" method="get" >

<input type="hidden" name="action" value="append" />
<input type="hidden" name="instance_id" value="<?= $_GET['instance_id'] ?>" />


<table class="libra">
<tr>
<td width="200"></td>
<td><input type="submit" name="submit" value="submit"  onclick="return confirm('Are you sure that you want to append this hotel to this resort?')"    /></td>
</tr>
</table>

</form>

</div>



<?



}
// END FUNCTION











switch ($_REQUEST['action']) {

	default;
		unset($_SESSION['hotel_download']);
		$template->head_control();
		display_description();
		$template->foot_control();
	break;

	case "confirm";
	
	
		// $_SESSION['hotel_download'];
		
		// lookup the resort tree ID
		$destinations = new destinations();
		$params['tree_id'] = $_GET['parent_tree_id'];
		$resort_values = $destinations->get_destination($params);
		
		if(!$resort_values){
			$my_message = array();
			$my_message['type'] = 'neg';
			$my_message['message'] = "Destination not found.";
			$GLOBALS['note'] = $my_message;
			$template->head_control();
			display_description();
			$template->foot_control();
		}else{
			$_SESSION['hotel_download']['parent_tree_id'] = $_GET['parent_tree_id'];
			
			$_SESSION['hotel_download_branch']['type_id'] = '3';
			$_SESSION['hotel_download_branch']['parent_id'] = $_SESSION['hotel_download']['parent_tree_id'];
			$_SESSION['hotel_download_branch']['lineage'] = $resort_values['lineage'].",".$resort_values['tree_id'];
			
			$template->head_control();
			display_confirm_page($resort_values);
			$template->foot_control();
		}
	break;

	case "append";

		$tree = new tree();
		$hotels = new hotels();
        $photos = new photos();
		$hotel_instances_you = new hotel_instances_you();
		
		
		// Insert branch into tree
		$tree_id = $tree->insert_branch($_SESSION['hotel_download_branch']);
		
		// Insert hotel info
		$_SESSION['hotel_download']['tree_id'] = $tree_id;
		$hotels->insert_hotel($_SESSION['hotel_download']);
		
		// Insert instance into the table hotel_instances_youtravel
		$params['tree_id'] = $tree_id;
		$params['hotel_name'] = $_SESSION['hotel_download']['name'];
		$params['hotel_id_youtravel'] = $_SESSION['hotel_download']['hotel_id_youtravel'];
		$hotel_instances_you->insert_linked_instance($params);
		unset($params);
		
		if(!empty($_GET['instance_id'])){
			// Deleted the flaged instance
			$hotel_instances_you->deleted_flagged_instance($_GET['instance_id']);
		}
		
		// Download photos
		foreach($_SESSION['hotel_download']['photos'] as $i => $url) {
			$filename = UPLOAD_ABS_TEMP.unique_id().'.jpg';
			$all_files[] = $filename;
      		copy($url, $filename);
      		// and thats it
    	}


    $uploader = new uploader();
    $photos = new photos();
		// Insert photo urls into DB and move 
		foreach($all_files as $photo_file){
  		// Large Image
  		// create a unique name
  		$unique_file_name_img = unique_id();
  		// Re-size image from uploaded image
  		$uploader->optimise_photo_image(FULL_HEIGHT, FULL_WIDTH, $photo_file, UPLOAD_ABS_TEMP, $unique_file_name_img, '.jpg');

  		// Thumbnail Image
  		// create a unique name
  		$unique_file_name_img_thumb = unique_id(); 
  		// Re-size image from uploaded image
  		$uploader->optimise_photo_image(THUMB_HEIGHT, THUMB_WIDTH, $photo_file, UPLOAD_ABS_TEMP, $unique_file_name_img_thumb, '.jpg');
		  
			$photo_insert = array();
			$photo_insert['tree_id'] = $tree_id;
			$photo_insert['photo_url_full'] = $unique_file_name_img.".jpg";
			$photo_insert['photo_url_thumb'] = $unique_file_name_img_thumb.".jpg";
		
			$photos->insert_photo($photo_insert);

			// Move images from temp to permament location
			rename(UPLOAD_ABS_TEMP.$photo_insert['photo_url_full'], UPLOAD_ABS_PERM.$photo_insert['photo_url_full']);
			rename(UPLOAD_ABS_TEMP.$photo_insert['photo_url_thumb'], UPLOAD_ABS_PERM.$photo_insert['photo_url_thumb']);		
      // remove downloaded original image
      unlink($photo_file);
		}
		
		// forward page header to prevent the data being loaded more then one
		header("Location: hotels.php?action=uploaded&tree_id=$tree_id");
		
		
	break;
	
	


}



?>